package com.dropdb.demo.controller.admin;

import com.dropdb.demo.bean.Permission;
import com.dropdb.demo.bean.Role;
import com.dropdb.demo.bean.bo.AdminRoleCreateBO;
import com.dropdb.demo.bean.bo.AdminRoleListBO;
import com.dropdb.demo.bean.bo.AdminRolePermissionsBO;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.service.RoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

@RestController
@RequestMapping("admin/role")
@Validated
public class AdminRoleController {

    @Autowired
    RoleService roleService;

    @GetMapping("list")
    @RequiresPermissions(value = {"*", "admin:role:list"}, logical = Logical.OR)
    public BaseRespVO list(@Valid AdminRoleListBO adminRoleListBo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BaseRespVO.error(502, bindingResult.getFieldError().getDefaultMessage());
        }
        return BaseRespVO.ok(roleService.list(adminRoleListBo));
    }

    @PostMapping("create")
    @RequiresPermissions(value = {"*", "admin:role:create"}, logical = Logical.OR)
    public BaseRespVO create(@Valid @RequestBody AdminRoleCreateBO adminRoleCreateBO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BaseRespVO.error(502, bindingResult.getFieldError().getDefaultMessage());
        }
        Role role = roleService.create(adminRoleCreateBO.getName(), adminRoleCreateBO.getDesc());
        if (null == role) {
            return BaseRespVO.error(640, "角色已经存在");
        }
        return BaseRespVO.ok(role);
    }

    @PostMapping("update")
    @RequiresPermissions(value = {"*", "admin:role:update"}, logical = Logical.OR)
    public BaseRespVO update(@RequestBody Role role) {
        int result = roleService.update(role);
        return 1 == result ? BaseRespVO.ok() : BaseRespVO.error(502, "系统内部错误");
    }

    @PostMapping("delete")
    @RequiresPermissions(value = {"*", "admin:role:delete"}, logical = Logical.OR)
    public BaseRespVO delete(@RequestBody Role role) {
        int result = roleService.delete(role);
        return 1 == result ? BaseRespVO.ok() : BaseRespVO.error(502, "系统内部错误");
    }

    @GetMapping("options")
    public BaseRespVO options() {
        return BaseRespVO.ok(roleService.options());
    }

    @GetMapping("permissions")
    @RequiresPermissions(value = {"*", "admin:role:permissions:get"}, logical = Logical.OR)
    public BaseRespVO permissions(@Min(1) Integer roleId) {
        return BaseRespVO.ok(roleService.permissions(roleId));
    }

    @PostMapping("permissions")
    @RequiresPermissions(value = {"*", "admin:role:permissions:update"}, logical = Logical.OR)
    public BaseRespVO permissions(@RequestBody AdminRolePermissionsBO bo) {
        roleService.permissions(bo);
        return BaseRespVO.ok();
    }
}
